package com.ps.service.setting.imp;


import java.util.List;
import com.ps.service.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;



import com.ps.dao.BaseDaoSupport;
import com.ps.dao.setting.FunctionModuleDAO;
import com.ps.dao.setting.RoleOfPermissionDAO;
import com.ps.pojo.setting.FunctionModule;
import com.ps.pojo.setting.Permission;
import com.ps.pojo.setting.RoleOfPermission;
import com.ps.service.setting.FunctionModuleService;
import com.ps.util.Kit;
import com.ps.view.tree.Item;
import com.ps.view.tree.Tree;

public class FunctionModuleServiceImp extends BaseServiceSupport<FunctionModule> implements
		FunctionModuleService {
	

	@Autowired
	private FunctionModuleDAO functionModuleDAO;
	
	@Autowired
	private RoleOfPermissionDAO roleOfPermissionDAO;
	
	@SuppressWarnings("unchecked")
	public String findAll2Json() {
		try {
			List<FunctionModule> list = functionModuleDAO.findAll();
			if (list != null && list.size() > 0) {
				for (FunctionModule fm : list) {
					fm.getPermissionList().addAll(fm.getPermissions());
				}
				return Kit.toJsonFieldsWithout(list);
			} else {
				return "";
			}
		} catch (Exception ex) {
			log.error("findAll2Json error !", ex);
			return "";
		} 
	}


	public String findAll2XmlTree() {
		Tree tree = new Tree();
		tree.setId("0");
		Item item = new Item();
		item.setId("-1");
		item.setOpen("1");
		item.setText("功能列表");
	    
		try {
			List<FunctionModule> list = functionModuleDAO.findAll();
			if (list != null && list.size() > 0) {
				for (FunctionModule fm : list) {
					Item mitem = new Item();
					mitem.setId("m_" + fm.getFunctionModuleId());	
					mitem.setText(fm.getName());
					for (Object obj : fm.getPermissions()) {
						Permission p = (Permission)obj;
						Item pitem = new Item();
						pitem.setId("p_" + p.getPermissionId());
						pitem.setText(p.getName());
						mitem.getItem().add(pitem);
					}
					item.getItem().add(mitem);
				}
				
			} 
			tree.getItem().add(item);
			return Kit.obj2XmlString(tree);
		} catch (Exception ex) {
			log.error("findAll2Json error !", ex);
			return Kit.obj2XmlString(tree);
		} 
	}

	public String findAllByRoleId2XmlTree(Integer roleId) {
		Tree tree = new Tree();
		tree.setId("0");
		Item item = new Item();
		item.setId("-1");
		item.setOpen("1");
		item.setText("功能列表");
	    
		try {
			List<FunctionModule> list = functionModuleDAO.findAll();
			List<RoleOfPermission> list_p = roleOfPermissionDAO.findByProperty(roleOfPermissionDAO.ROLE_ID, roleId);
			
			if (list != null && list.size() > 0) {
				for (FunctionModule fm : list) {
					Item mitem = new Item();
					mitem.setId("m_" + fm.getFunctionModuleId());	
					mitem.setText(fm.getName());
					for (Object obj : fm.getPermissions()) {
						Permission p = (Permission)obj;
						Item pitem = new Item();
						pitem.setId("p_" + p.getPermissionId());
						pitem.setText(p.getName());
						
						
						if (isExistsPermissionId(list_p,  p.getPermissionId())) {
							pitem.setChecked("true");
							mitem.setOpen("true");
						}
						mitem.getItem().add(pitem);
					}
					item.getItem().add(mitem);
				}
				
			} 
			tree.getItem().add(item);
			return Kit.obj2XmlString(tree);
		} catch (Exception ex) {
			log.error("findAll2Json error !", ex);
			return Kit.obj2XmlString(tree);
		} 
	}
	
	
	private boolean isExistsPermissionId(List<RoleOfPermission> list_p, Integer permissionId) {
		if (list_p == null || list_p.size() == 0) {
			return false;
		}
		
		for (RoleOfPermission rop : list_p) {
			if (rop.getPermissionId().intValue() == permissionId.intValue()) {
				return true;
			}
		}
		
		return false;
	}
	
	@Override
	public BaseDaoSupport<FunctionModule> getBaseDao() {
		// TODO Auto-generated method stub
		return functionModuleDAO;
	}
	
	
	

}
